# ### zipfile 压缩模块
import zipfile

# (1) 压缩文件
""" zipfile.ZIP_DEFLATED 压缩减少空间 """
# 创建压缩包
zf = zipfile.ZipFile("./11_压缩模块zipfile/ceshi111.zip", "w", zipfile.ZIP_DEFLATED)
# 写入文件
'''write(路径,别名)'''
zf.write("09_json模块.py", "json")
zf.write("10_时间模块time.py", "bunzip2")
zf.write("01_推导式.py", "tmp/cat")
# 关闭文件
zf.close()

# (2) 解压文件
zf = zipfile.ZipFile("./11_压缩模块zipfile/ceshi111.zip", "r")
# 解压单个文件
"""extract(文件,路径)"""
# zf.extract("json","ceshi111")
# 解压所有文件
zf.extractall("./11_压缩模块zipfile/ceshi222")
zf.close()

# (3) 追加文件
zf = zipfile.ZipFile("./11_压缩模块zipfile/ceshi111.zip", "a", zipfile.ZIP_DEFLATED)
zf.write("02_推导式练习题.py", "chmod")
zf.close()

# 用with来简化操作
with zipfile.ZipFile("./11_压缩模块zipfile/ceshi111.zip", "a", zipfile.ZIP_DEFLATED) as zf:
	zf.write("02_推导式练习题.py", "chmod123456")

# (4) 查看文件
with zipfile.ZipFile("./11_压缩模块zipfile/ceshi111.zip", "r") as zf:
	lst = zf.namelist()
	print(lst)
